package cn.engage.text;

import cn.engage.util.ESClientUtil;
import org.elasticsearch.action.get.GetRequestBuilder;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateRequestBuilder;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

public class EsTest {

  @Test
  public void testAdd() throws Exception{
    // 获取ES连接
    TransportClient client = ESClientUtil.getClient();
    Map map = new HashMap<>();
    map.put("id",1);
    map.put("name","可莉");
    map.put("age",8);
    IndexRequestBuilder source = client.prepareIndex("pethome", "employee", "1").setSource(map);
    System.out.println(source.get());
  }

  @Test
  public void testGet() throws Exception{
    TransportClient client = ESClientUtil.getClient();
    GetResponse documentFields = client.prepareGet("pethome", "employee", "1").get();
    System.out.println(documentFields);

  }

  @Test
  public void testupdate() throws Exception{
    // 获取ES连接
    TransportClient client = ESClientUtil.getClient();
    Map map = new HashMap<>();
    map.put("id",1);
    map.put("name","七七");
    map.put("age",9);
    UpdateRequestBuilder updateRequestBuilder = client.prepareUpdate("pethome", "employee", "1").setDoc(map);
    System.out.println(updateRequestBuilder.get());
  }

  @Test
  public void testAddResource() throws Exception{
    // 获取ES连接
    for (int i = 2; i < 100; i++) {
      // 2.指定要操作的索引库，类型
      IndexRequestBuilder indexRequestBuilder = ESClientUtil.getClient().prepareIndex("pethome", "employee");
      indexRequestBuilder.setId(String.valueOf(i));
      Map<String, Object> map = new HashMap<>();
      map.put("id", i);
      if (i % 3==0){
        map.put("name", "七七--" + i);
      }else if(i % 3==1){
        map.put("name", "可莉--" + i);
      }else {
        map.put("name", "刻晴--" + i);
      }
      map.put("age", i);
      map.put("sex", i%2==1);
      // 3.放入新增数据，同时获取到执行结果
      IndexResponse indexResponse = indexRequestBuilder.setSource(map).get();
      System.out.println(indexResponse);
    }
  }

  /**
   * - 查询employee表
   * - name包含：我在源码   must->match
   * - age在1~12之间  filter->range
   * - sex=1 filter
   * - 每页大小2 size=10
   * - 从3页开始查 from= 0~9 10~19 20 ~29
   * - 按照age倒序 sort=desc
   */
  @Test
  public void testGets() throws Exception{
    // 获取连接
    TransportClient client = ESClientUtil.getClient();
    SearchRequestBuilder searchRequestBuilder = client.prepareSearch("pethome").setTypes("employee");
    // 写查询条件
    BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery()
      .must(QueryBuilders.matchQuery("name", "七七"))
      .filter(QueryBuilders.termQuery("sex", true))
      .filter(QueryBuilders.rangeQuery("age").gte(1).lte(12));

    searchRequestBuilder.setQuery(queryBuilder);
    // 每页十条
    searchRequestBuilder.setSize(10);
    // 从第1页开始
    searchRequestBuilder.setFrom(0);
    // 年龄倒叙
    searchRequestBuilder.addSort("age", SortOrder.DESC);
    // 获取值
    SearchResponse searchResponse = searchRequestBuilder.get();
    SearchHit[] hits = searchResponse.getHits().getHits();
    for (SearchHit hit : hits) {
      System.out.println(hit.getSourceAsMap());
    }
  }
}
